<template>
	<view class="page">
		<view class="danger-form">
			<uni-forms :modelValue="formData" :rules="formRules" ref="formRef" label-position="top" label-width="150">
				<uni-forms-item name="danger_no" label="编号">
					<uni-easyinput :disabled="!isEditable" type="text" v-model="formData.danger_no" placeholder="自动生成" />
				</uni-forms-item>
				<uni-forms-item name="danger_code" label="ID">
					<uni-easyinput :disabled="!isEditable" type="text" v-model="formData.danger_code" placeholder="自动生成" />
				</uni-forms-item>
				<uni-forms-item required name="danger_name" label="危险源名称">
					<uni-easyinput :disabled="!isEditable" type="text" v-model="formData.danger_name" placeholder="请输入危险源名称" />
				</uni-forms-item>
				<uni-forms-item required name="danger_dept" label="危险源管理主责部门">
					<uni-data-select :disabled="!isEditable" v-model="formData.danger_dept" :localdata="deptList"></uni-data-select>
				</uni-forms-item>
				<uni-forms-item required name="danger_major" label="重大危险源">
					<uni-data-select :disabled="!isEditable" v-model="formData.danger_major" :localdata="majorDangerList"></uni-data-select>
				</uni-forms-item>

				<view class="u-m-b-44">
					<uni-section title="危险源识别" type="line" padding="20rpx">
						<uni-forms-item required name="danger_src" label="危险源来源">
							<uni-data-select :disabled="!isEditable" v-model="formData.danger_src" :localdata="dangerSrcList"></uni-data-select>
						</uni-forms-item>
						<uni-forms-item required name="danger_result" label-width="350" label="可能导致的后果(事故,症候,一般事件等)">
							<uni-easyinput :disabled="!isEditable" type="textarea" v-model="formData.danger_result" placeholder="请输入可能导致的后果" />
						</uni-forms-item>
					</uni-section>
				</view>

				<view class="u-m-b-44">
					<uni-section title="风险分析和风险评价分级" type="line" padding="20rpx">
						<uni-forms-item required name="danger_control_measure_now" label-width="350" label="现有风险控制措施(针对危险源已有的规章制度和操作规程、技术、培训等)">
							<uni-easyinput :disabled="!isEditable" type="textarea" v-model="formData.danger_control_measure_now" placeholder="请输入现有风险控制措施" />
						</uni-forms-item>
					</uni-section>
				</view>

				<view class="u-m-b-44">
					<uni-section title="风险分级" type="line" padding="20rpx">
						<uni-forms-item required name="danger_grade_possibility" label="可能性">
							<uni-data-select
								:disabled="!isEditable"
								v-model="formData.danger_grade_possibility"
								:localdata="possibilityList"
								@change="onGradePossibilityChange"
							></uni-data-select>
							<!-- <uni-easyinput :disabled="!isEditable" type="text" v-model="formData.danger_grade_possibility" placeholder="请输入可能性" /> -->
						</uni-forms-item>
						<uni-forms-item required name="danger_grade_severity" label="严重性">
							<uni-data-select
								:disabled="!isEditable"
								v-model="formData.danger_grade_severity"
								:localdata="severityList"
								@change="onGradeSeverityChange"
							></uni-data-select>
							<!-- <uni-easyinput :disabled="!isEditable" type="text" v-model="formData.danger_grade_severity" placeholder="请输入严重性" /> -->
						</uni-forms-item>
						<uni-forms-item required name="danger_grade_value" label="风险值">
							<uni-easyinput :disabled="!isEditable" type="text" v-model="formData.danger_grade_value" placeholder="请输入风险值" />
						</uni-forms-item>
						<uni-forms-item required name="danger_evaluate_grade" label="风险评价分级">
							<uni-data-select :disabled="!isEditable" v-model="formData.danger_evaluate_grade" :localdata="dangerEvaluateGradeList"></uni-data-select>
						</uni-forms-item>
					</uni-section>
				</view>

				<uni-forms-item required name="danger_control_measure" label-width="350" label="风险控制措施(如风险处于可接受,可填写不涉及)">
					<uni-easyinput :disabled="!isEditable" type="textarea" v-model="formData.danger_control_measure" placeholder="请输入风险控制措施" />
				</uni-forms-item>

				<view class="u-m-b-44">
					<uni-section title="剩余风险" type="line" padding="20rpx">
						<uni-forms-item required name="rest_danger_possibility" label="可能性">
							<uni-data-select
								:disabled="!isEditable"
								v-model="formData.rest_danger_possibility"
								:localdata="possibilityList"
								@change="onRestPossibilityChange"
							></uni-data-select>
							<!-- <uni-easyinput :disabled="!isEditable" type="text" v-model="formData.rest_danger_possibility" placeholder="请输入可能性" /> -->
						</uni-forms-item>
						<uni-forms-item required name="rest_danger_severity" label="严重性">
							<uni-data-select
								:disabled="!isEditable"
								v-model="formData.rest_danger_severity"
								:localdata="severityList"
								@change="onRestSeverityChange"
							></uni-data-select>
							<!-- <uni-easyinput :disabled="!isEditable" type="text" v-model="formData.rest_danger_severity" placeholder="请输入严重性" /> -->
						</uni-forms-item>
						<uni-forms-item required name="rest_danger_value" label="风险值">
							<uni-easyinput :disabled="!isEditable" type="text" v-model="formData.rest_danger_value" placeholder="请输入风险值" />
						</uni-forms-item>
						<uni-forms-item required name="rest_danger_evaluate_grade" label="风险评价分级">
							<uni-data-select :disabled="!isEditable" v-model="formData.rest_danger_evaluate_grade" :localdata="dangerEvaluateGradeList"></uni-data-select>
						</uni-forms-item>
					</uni-section>
				</view>

				<uni-forms-item required name="derive_new_danger_src" label-width="350" label="是否衍生新的危险源(如是,填写新危险源名称和编号)">
					<uni-easyinput :disabled="!isEditable" type="textarea" v-model="formData.derive_new_danger_src" placeholder="请输入是否衍生新的危险源" />
				</uni-forms-item>
			</uni-forms>

			<button v-if="isEditable" @tap="submitDangerForm" class="danger-submit" hover-class="hover-class">提交</button>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			formType: 'add',
			dangerStatus: 0,
			formData: {
				danger_no: '',
				danger_code: '',
				danger_name: '',
				danger_dept: '',
				danger_major: '',
				danger_src: '',
				danger_result: '',
				danger_control_measure_now: '',
				danger_grade_possibility: '',
				danger_grade_severity: '',
				danger_grade_value: '',
				danger_evaluate_grade: '',
				danger_control_measure: '',
				rest_danger_possibility: '',
				rest_danger_severity: '',
				rest_danger_value: '',
				rest_danger_evaluate_grade: '',
				derive_new_danger_src: ''
			},
			deptList: [],
			majorDangerList: [],
			dangerSrcList: [],
			dangerEvaluateGradeList: [],
			possibilityList: [{ value: 1, text: '1' }, { value: 2, text: '2' }, { value: 3, text: '3' }, { value: 4, text: '4' }, { value: 5, text: '5' }],
			severityList: [{ value: 1, text: '1' }, { value: 2, text: '2' }, { value: 3, text: '3' }, { value: 4, text: '4' }, { value: 5, text: '5' }],
			formRules: {
				danger_name: {
					rules: [
						{
							required: true,
							errorMessage: '请填写危险源名称'
						}
					]
				},
				danger_dept: {
					rules: [
						{
							required: true,
							errorMessage: '请选择危险源管理主责部门'
						}
					]
				},
				danger_major: {
					rules: [
						{
							required: true,
							errorMessage: '请选择重大危险源'
						}
					]
				},
				danger_src: {
					rules: [
						{
							required: true,
							errorMessage: '请选择危险源来源'
						}
					]
				},
				danger_result: {
					rules: [
						{
							required: true,
							errorMessage: '请填写可能导致的后果'
						}
					]
				},
				danger_control_measure_now: {
					rules: [
						{
							required: true,
							errorMessage: '请填写现有风险控制措施'
						}
					]
				},
				danger_grade_possibility: {
					rules: [
						{
							required: true,
							errorMessage: '请填写可能性'
						}
					]
				},
				danger_grade_severity: {
					rules: [
						{
							required: true,
							errorMessage: '请填写严重性'
						}
					]
				},
				danger_grade_value: {
					rules: [
						{
							required: true,
							errorMessage: '请填写风险值'
						}
					]
				},
				danger_evaluate_grade: {
					rules: [
						{
							required: true,
							errorMessage: '请选择风险评价分级'
						}
					]
				},
				danger_control_measure: {
					rules: [
						{
							required: true,
							errorMessage: '请填写风险控制措施'
						}
					]
				},
				rest_danger_possibility: {
					rules: [
						{
							required: true,
							errorMessage: '请填写可能性'
						}
					]
				},
				rest_danger_severity: {
					rules: [
						{
							required: true,
							errorMessage: '请填写严重性'
						}
					]
				},
				rest_danger_value: {
					rules: [
						{
							required: true,
							errorMessage: '请填写风险值'
						}
					]
				},
				rest_danger_evaluate_grade: {
					rules: [
						{
							required: true,
							errorMessage: '请选择风险评价分级'
						}
					]
				},
				derive_new_danger_src: {
					rules: [
						{
							required: true,
							errorMessage: '请填写是否衍生新的危险源'
						}
					]
				}
			}
		}
	},
	computed: {
		isEditable() {
			return this.formType === 'add' || this.dangerStatus !== 1
		}
	},
	onLoad(options) {
		console.log('danger form: ', options)
		this.formType = options.type
		this.dangerStatus = +options.status
		this.formData.danger_code = options.id || 0

		// if (this.formType === 'add') {
		// 	this.formData = Object.assign(this.formData, {
		// 		danger_name: '危险源名称测试01',
		// 		danger_dept: 2,
		// 		danger_major: 1,
		// 		danger_src: 2,
		// 		danger_result: '可能导致的后果测试01',
		// 		danger_control_measure_now: '现有风险控制措施测试01',
		// 		danger_grade_possibility: '可能性测试01',
		// 		danger_grade_severity: '严重性测试01',
		// 		danger_grade_value: '风险值测试01',
		// 		danger_evaluate_grade: 1,
		// 		danger_control_measure: '风险控制措施测试02',
		// 		rest_danger_possibility: '可能性测试02',
		// 		rest_danger_severity: '严重性测试02',
		// 		rest_danger_value: '风险值测试01',
		// 		rest_danger_evaluate_grade: 1,
		// 		derive_new_danger_src: '衍生新的危险源测试01'
		// 	})
		// }

		Promise.all([this.getListOneOffice(), this.getRiskDict('yn'), this.getRiskDict('source_risk'), this.getRiskDict('level_risk')])
	},
	onReady() {
		this.$refs.formRef.setRules(this.formRules)

		if (this.formType === 'add') {
			uni.setNavigationBarTitle({
				title: `增加危险源`
			})
		} else {
			uni.setNavigationBarTitle({
				title: `编辑危险源`
			})
			this.getDangerDetail()
		}
	},
	methods: {
		onGradePossibilityChange(e) {
			if (this.formData.danger_grade_severity) {
				this.formData.danger_grade_value = isNaN(e * this.formData.danger_grade_severity) ? 0 : e * this.formData.danger_grade_severity
			}
		},
		onGradeSeverityChange(e) {
			if (this.formData.danger_grade_possibility) {
				this.formData.danger_grade_value = isNaN(e * this.formData.danger_grade_possibility) ? 0 : e * this.formData.danger_grade_possibility
			}
		},
		onRestPossibilityChange(e) {
			if (this.formData.rest_danger_severity) {
				this.formData.rest_danger_value = isNaN(e * this.formData.rest_danger_severity) ? 0 : e * this.formData.rest_danger_severity
			}
		},
		onRestSeverityChange(e) {
			if (this.formData.rest_danger_possibility) {
				this.formData.rest_danger_value = isNaN(e * this.formData.rest_danger_possibility) ? 0 : e * this.formData.rest_danger_possibility
			}
		},
		async getDangerDetail() {
			const params = {
				id: this.formData.danger_code
			}

			try {
				const res = await uni.$uu.Api.getDangerDetail(params)

				console.log(res)

				if (res.code === 100) {
					this.formData = Object.assign(this.formData, {
						danger_no: res.content.no,
						danger_name: res.content.name,
						danger_dept: res.content.dept,
						danger_major: res.content.isBig,
						danger_src: res.content.source,
						danger_result: res.content.result,
						danger_control_measure_now: res.content.measure,
						danger_grade_possibility: +res.content.riskPossibility,
						danger_grade_severity: +res.content.riskSeverity,
						danger_grade_value: +res.content.riskValue,
						danger_evaluate_grade: res.content.riskLevel,
						danger_control_measure: res.content.controlMeasure,
						rest_danger_possibility: +res.content.riskOverPossibility,
						rest_danger_severity: +res.content.riskOverSeverity,
						rest_danger_value: +res.content.riskOverValue,
						rest_danger_evaluate_grade: res.content.riskOverLevel,
						derive_new_danger_src: res.content.newSource
					})
				}
			} catch (err) {
				console.log(err)
			}
		},
		async getListOneOffice() {
			try {
				const res = await uni.$uu.Api.getListOneOffice()

				console.log(res)

				if (res.code === 100) {
					this.deptList = res.content.map(_ => {
						return {
							text: _.name,
							value: _.id
						}
					})
				}
			} catch (e) {
				console.log(e)
			}
		},
		handleDictData(type, data) {
			switch (type) {
				case 'yn':
					this.majorDangerList = data.map(_ => {
						return {
							text: _.name,
							value: +_.value
						}
					})
					break
				case 'source_risk':
					this.dangerSrcList = data.map(_ => {
						return {
							text: _.name,
							value: +_.value
						}
					})
					break
				case 'level_risk':
					this.dangerEvaluateGradeList = data.map(_ => {
						return {
							text: _.name,
							value: +_.value
						}
					})
					break
				default:
					break
			}
		},
		async getRiskDict(type) {
			const params = {
				type
			}

			try {
				const res = await uni.$uu.Api.getRiskDict(params)
				console.log(res)
				if (res.code === 100) {
					this.handleDictData(type, res.content)
				}
			} catch (e) {
				console.log(e)
			}
		},
		submitDangerForm(e) {
			console.log('submitDangerForm:', this.formData)
			this.$refs.formRef
				.validate()
				.then(res => {
					console.log('表单数据信息：', res)
					this.addDanger()
				})
				.catch(err => {
					console.log('表单错误信息：', err)
				})
		},
		async addDanger() {
			let params = {
				name: this.formData.danger_name,
				dept: this.formData.danger_dept,
				isBig: this.formData.danger_major,
				source: this.formData.danger_src,
				result: this.formData.danger_result,
				measure: this.formData.danger_control_measure_now,
				riskPossibility: this.formData.danger_grade_possibility,
				riskSeverity: this.formData.danger_grade_severity,
				riskValue: this.formData.danger_grade_value,
				riskLevel: this.formData.danger_evaluate_grade,
				controlMeasure: this.formData.danger_control_measure,
				riskOverPossibility: this.formData.rest_danger_possibility,
				riskOverSeverity: this.formData.rest_danger_severity,
				riskOverValue: this.formData.rest_danger_value,
				riskOverLevel: this.formData.rest_danger_evaluate_grade,
				newSource: this.formData.derive_new_danger_src
			}

			if (this.formType === 'edit') {
				params['id'] = this.formData.danger_code
			}

			const ApiName = this.formType === 'edit' ? 'editDanger' : 'addDanger'

			try {
				const res = await uni.$uu.Api[ApiName](params)

				console.log(res)

				if (res.code === 100) {
					uni.showToast({
						title: '提交成功',
						success: () => {
							const pages = getCurrentPages()
							if (pages.length > 1) {
								const prevPage = pages[pages.length - 2]
								prevPage.$vm.getData(1)
							}
							uni.navigateBack()
						}
					})
				}
			} catch (err) {
				console.log(err)
			}
		}
	}
}
</script>

<style lang="scss" scoped>
@import '~@/common/app.scss';
@import './index.scss';
</style>
